

var express = require("express")
	, app = express()
	, fs = require('fs')
  	, path = require('path');

// need it...  
app.use(express.bodyParser()); //.urlencoded({ extended: false })

exports.Save = function(req, res, callback) {
 	var mdStr = req.body.md;
 	var content = '<html><head><title>API</title><link rel="stylesheet" href="../css/bootstrap.min.css" /><link rel="stylesheet" href="../js/plugins/editormd/editormd.css" /></head><body>';
 	var htmlStr = content + '<div class="markdown-body editormd-preview-container" previewcontainer="true" style="padding: 20px;">' + req.body.html + '</div></body></html>';

 	var fileName = "public/APIDocument/" + (new Date().getTime()).toString();


	fs.writeFile(fileName + '.md', mdStr, function (err) {
        if (err){
        	callback({ code: -100, msg: err.Error, value: null});
	    } else {
	        fs.writeFile(fileName + '.html', htmlStr, function (err) {
		        if (err){
		        	callback({ code: -50, msg: err.Error, value: null});
			    } else {
        			callback({ code: 100, msg: "OK", value: fileName});
			    }
		    });
	    }
    });
}


// fs.appendFile("public/APIDocument/" + (new Date().getTime()) + ".md", '{ "name": "yanggenshui" }', function(err) {
//     if(err) {
//         console.log(err);
//     } else {
//         console.log("The file was saved!");
//     }
// }); 